package com.jss.lanouhn.fristapp.adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.modles.FreeFragmentRightInfo;
import com.squareup.picasso.Picasso;

/**
 * Created by lanouhn on 16/9/3.
 */
public class FreeRightTopRvAdapter extends RecyclerView.Adapter<FreeRightTopRvAdapter.TopRvViewHolder>{
    private Context context;
    private FreeFragmentRightInfo.DataBean.RecordsBean.ItemBean info;
    DisplayMetrics dm;

    public FreeRightTopRvAdapter(Context context, FreeFragmentRightInfo.DataBean.RecordsBean.ItemBean info) {
        this.context = context;
        this.info = info;
        dm = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    }

    @Override
    public TopRvViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_free_item_top_rv,null);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((dm.widthPixels ), (dm.widthPixels - dip2px(20)));
        view.setLayoutParams(lp);
        TopRvViewHolder holder=new TopRvViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(TopRvViewHolder holder, int position) {
        holder.tv.setText(info.getTitle());
        Picasso.with(context).load(info.getImage_urls().get(position)).into(holder.iv);
    }

    @Override
    public int getItemCount() {
        return info.getImage_urls().size();
    }

    public class TopRvViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv;
        private TextView tv;
       public TopRvViewHolder(View itemView) {
           super(itemView);
           tv= (TextView) itemView.findViewById(R.id.tv_search_above_iv);
           iv= (ImageView) itemView.findViewById(R.id.iv_item_free_big);
       }
   }
    int dip2px(float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
